﻿namespace NTrace
{
  using System;
  using ICSharpCode.NRefactory.PrettyPrinter;

  public class CSharpLanguage : TargetLanguage
  {
    public override IOutputAstVisitor CreatePrinter()
    {
      return new CSharpOutputVisitor();
    }

    public override OriginalSourceWrapper CreateSourceWrapper(String fileName, System.IO.TextWriter writer)
    {
      return new CSharpSourceWrapper(this, fileName, writer);
    }

    public override String StartSourceWrapper(String fileName, int line)
    {
      return String.Format("#line {0} \"{1}\"", line, fileName);
    }

    public override String StopSourceWrapper()
    {
      return "#line default";
    }

    /// <summary>
    /// The number of characters to trim off of a statement to remove delimiters
    /// </summary>
    /// <value>A <see cref="System.Int32"/> indicating how many characters to trim off of a statement to remove delimiters.</value>
    public virtual int StatementEndOffset { get { return 1; } }

    private class CSharpSourceWrapper : OriginalSourceWrapper
    {
      private CSharpLanguage owner;
      private System.IO.TextWriter writer;
      public CSharpSourceWrapper(CSharpLanguage owner, String fileName, System.IO.TextWriter writer) : base(fileName, writer)
      {
        this.owner = owner;
        this.writer = writer;
        this.writer.WriteLine(owner.StartSourceWrapper(fileName, 1));
      }

      public override void Dispose()
      {
        this.writer.WriteLine(this.owner.StopSourceWrapper());
      }
    }
  }
}
